const express = require('express');
const router = express.Router();

// 获取系统信息
router.get('/info', async (req, res) => {
  try {
    const info = await req.docker.info();
    res.json(info);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 获取Docker版本信息
router.get('/version', async (req, res) => {
  try {
    const version = await req.docker.version();
    res.json(version);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

// 获取系统资源使用情况
router.get('/usage', async (req, res) => {
  try {
    const info = await req.docker.info();
    const containers = await req.docker.listContainers({ all: true });
    const images = await req.docker.listImages();
    
    res.json({
      containers: {
        total: containers.length,
        running: containers.filter(c => c.State === 'running').length,
        paused: containers.filter(c => c.State === 'paused').length,
        stopped: containers.filter(c => c.State !== 'running').length
      },
      images: {
        total: images.length
      },
      system: {
        architecture: info.Architecture,
        os: info.OperatingSystem,
        kernelVersion: info.KernelVersion,
        serverVersion: info.ServerVersion,
        memoryTotal: info.MemTotal,
        cpuCores: info.NCPU
      }
    });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

module.exports = router;
